﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CBMS.Interfaces;
using CBMS.Business;
using CBMS.Resources;
using CBMS.Objects;
namespace CBMS
{
    public partial class frmEmpList : Form
    {
        private string _sortField = "FirstName";
        private string _sortExp = "ASC";
        public static IEmployeeRepository empRes { get; set; }
        public frmEmpList()
        {
            InitializeComponent();
            ucPager1.PageSize = 2;
        }

        private void frmEmpList_Load(object sender, EventArgs e)
        {
            empRes = new EmployeeRepository();
            LoadData();

        }
        private void LoadData()
        {
            int toTal = 0;
            IList<EmployeeDTO> lstObject = empRes.SearchEmployeesBySomeCondition(" where 1=1", ucPager1.PageIndex, ucPager1.PageSize, _sortField, _sortExp, ref toTal);
            try
            {
                dataGridView1.Rows.Clear();
                foreach (EmployeeDTO ps in lstObject)
                {
                    dataGridView1.Rows.Add(ps.EmployeeId, ps.FirstName);
                }
            }
            catch
            {
            }

            ucPager1.TotalRecord = toTal;
            ucPager1.SelectRecord = lstObject.Count;
            ucPager1.SetPaging();

        }

        private void ucPager1_PageIndexChanging(object obj, CustomControls.PageIndexChangeEventArgs e)
        {
            ucPager1.PageIndex = e.newPageIndex;
            LoadData();
        }

        private void ucPager1_PageSizeChanging(object obj, CustomControls.PageSizeChangeEventArgs e)
        {
            ucPager1.PageIndex = 1;
            ucPager1.PageSize = e.newPageSize;
            LoadData();
        }
        private string ConvertSortIndexToField(int index)
        {
            if (index == 0)
                return "EmployeeId";
            return "FirstName";
        }
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string newsortField = ConvertSortIndexToField(e.ColumnIndex);
            if (newsortField == _sortField)
            {
                if (_sortExp == "ASC")
                    _sortExp = "DESC";
                else _sortExp = "ASC";
            }
            else
            {
                _sortField = newsortField;
                _sortExp = "ASC";
            }
            LoadData();  
        }

      
    }
}
